package singleton;

/**
 * 懒汉式
 * 唯一缺点: 存在线程不安全问题
 * 可以通过synchronized来解决,但是效率也下来了
 */
public class Mgr03 {

    private static Mgr03 INSTANCE;

    private Mgr03() {};

    public static synchronized Mgr03 getInstance() {
        if(null == INSTANCE){
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new Mgr03();
        }
        return INSTANCE;
    }

    public void m() {
        System.out.println("m");
    }

    public static void main(String[] args) {
        for(int i = 0; i < 100; i++){
            new Thread(() -> System.out.println(Mgr03.getInstance().hashCode())).start();
        }
    }

}
